package com.example.shuiyin.dto;

import com.example.shuiyin.entity.ProcessRecord;
import com.example.shuiyin.enums.MediaType;
import com.example.shuiyin.enums.ProcessStatus;
import com.example.shuiyin.enums.ProcessType;
import com.example.shuiyin.enums.SourceType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ProcessRecordDto {
    private Long id;
    private String fileId;
    private String fileName;
    private String processType;
    private String status;
    private LocalDateTime createTime;
    private LocalDateTime completeTime;
    private String resultFileId;
    private String resultUrl;
    private String mediaType;

    public static Builder builder() {
        return new Builder();
    }
    
    public static ProcessRecordDto fromEntity(ProcessRecord record) {
        if (record == null) {
            return null;
        }
        
        return ProcessRecordDto.builder()
                .id(record.getId())
                .fileId(record.getFileId())
                .fileName(record.getFileName())
                .processType(record.getProcessType() != null ? record.getProcessType().name() : null)
                .status(record.getStatus() != null ? record.getStatus().name() : null)
                .createTime(record.getCreateTime())
                .completeTime(record.getCompleteTime())
                .resultFileId(record.getResultFileId())
                .resultUrl(record.getResultUrl())
                .mediaType(record.getMediaType() != null ? record.getMediaType().name() : null)
                .build();
    }

    public void setMediaType(MediaType mediaType) {
        this.mediaType = mediaType != null ? mediaType.name() : null;
    }

    public void setMediaType(String mediaType) {
        this.mediaType = mediaType;
    }

    public void setProcessType(ProcessType processType) {
        this.processType = processType != null ? processType.name() : null;
    }
    
    public void setProcessType(String processType) {
        this.processType = processType;
    }
    
    public void setStatus(ProcessStatus status) {
        this.status = status != null ? status.name() : null;
    }
    
    public void setStatus(String status) {
        this.status = status;
    }

    public static class Builder {
        private final ProcessRecordDto dto;

        private Builder() {
            dto = new ProcessRecordDto();
        }

        public Builder id(Long id) {
            dto.setId(id);
            return this;
        }

        public Builder fileId(String fileId) {
            dto.setFileId(fileId);
            return this;
        }

        public Builder fileName(String fileName) {
            dto.setFileName(fileName);
            return this;
        }

        public Builder processType(String processType) {
            dto.setProcessType(processType);
            return this;
        }

        public Builder processType(ProcessType processType) {
            if (processType != null) {
                dto.setProcessType(processType.name());
            } else {
                setNullProcessType();
            }
            return this;
        }
        
        private void setNullProcessType() {
            dto.processType = null;
        }

        public Builder status(String status) {
            dto.setStatus(status);
            return this;
        }
        
        public Builder status(ProcessStatus status) {
            if (status != null) {
                dto.setStatus(status.name());
            } else {
                setNullStatus();
            }
            return this;
        }
        
        private void setNullStatus() {
            dto.status = null;
        }

        public Builder createTime(LocalDateTime createTime) {
            dto.setCreateTime(createTime);
            return this;
        }

        public Builder completeTime(LocalDateTime completeTime) {
            dto.setCompleteTime(completeTime);
            return this;
        }

        public Builder resultFileId(String resultFileId) {
            dto.setResultFileId(resultFileId);
            return this;
        }

        public Builder resultUrl(String resultUrl) {
            dto.setResultUrl(resultUrl);
            return this;
        }

        public Builder mediaType(MediaType mediaType) {
            if (mediaType != null) {
                dto.setMediaType(mediaType.name());
            } else {
                setNullMediaType();
            }
            return this;
        }
        
        private void setNullMediaType() {
            dto.mediaType = null;
        }

        public Builder mediaType(String mediaType) {
            dto.setMediaType(mediaType);
            return this;
        }

        public ProcessRecordDto build() {
            return dto;
        }
    }
} 